package goodclass

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/goodclass"
	goodclassReq "github.com/flipped-aurora/gin-vue-admin/server/model/goodclass/request"
	"gorm.io/gorm"
)

type LsGoodClassService struct{}

// CreateLsGoodClass 创建lsGoodClass表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodClassService *LsGoodClassService) CreateLsGoodClass(lsGoodClass *goodclass.LsGoodClass) (err error) {
	err = global.GVA_DB.Create(lsGoodClass).Error
	return err
}

// DeleteLsGoodClass 删除lsGoodClass表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodClassService *LsGoodClassService) DeleteLsGoodClass(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&goodclass.LsGoodClass{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&goodclass.LsGoodClass{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteLsGoodClassByIds 批量删除lsGoodClass表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodClassService *LsGoodClassService) DeleteLsGoodClassByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&goodclass.LsGoodClass{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&goodclass.LsGoodClass{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateLsGoodClass 更新lsGoodClass表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodClassService *LsGoodClassService) UpdateLsGoodClass(lsGoodClass goodclass.LsGoodClass) (err error) {
	err = global.GVA_DB.Model(&goodclass.LsGoodClass{}).Where("id = ?", lsGoodClass.ID).Updates(&lsGoodClass).Error
	return err
}

// GetLsGoodClass 根据ID获取lsGoodClass表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodClassService *LsGoodClassService) GetLsGoodClass(ID string) (lsGoodClass goodclass.LsGoodClass, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&lsGoodClass).Error
	return
}

// GetLsGoodClassInfoList 分页获取lsGoodClass表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodClassService *LsGoodClassService) GetLsGoodClassInfoList(info goodclassReq.LsGoodClassSearch) (list []goodclass.LsGoodClass, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&goodclass.LsGoodClass{})
	var lsGoodClasss []goodclass.LsGoodClass
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = global.GVA_DB.Where("pid=?", 0).Find(&lsGoodClasss).Error
	childrenList := lsGoodClassService.GetLsGoodsClassChildrenList(global.GVA_DB, lsGoodClasss)
	return childrenList, total, err
}
func (lsGoodClassService *LsGoodClassService) GetLsGoodClassPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
func (lsGoodsClassService *LsGoodClassService) GetLsGoodsClassChildrenList(db *gorm.DB, class []goodclass.LsGoodClass) []goodclass.LsGoodClass {

	var IsGoodsClass []goodclass.LsGoodClass
	var g []goodclass.LsGoodClass
	for _, lsGoodsClass := range class {
		db.Where("pid=?", lsGoodsClass.ID).Find(&IsGoodsClass)
		g = append(g, goodclass.LsGoodClass{
			GVA_MODEL: global.GVA_MODEL{
				ID:        lsGoodsClass.ID,
				CreatedAt: lsGoodsClass.CreatedAt,
				UpdatedAt: lsGoodsClass.UpdatedAt,
				DeletedAt: lsGoodsClass.DeletedAt,
			},
			ClassName: lsGoodsClass.ClassName,
			Pid:       lsGoodsClass.Pid,
			ClassIcon: lsGoodsClass.ClassIcon,
			Sort:      lsGoodsClass.Sort,
			Enable:    lsGoodsClass.Enable,
			CreatedBy: lsGoodsClass.CreatedBy,
			UpdatedBy: lsGoodsClass.UpdatedBy,
			DeletedBy: lsGoodsClass.DeletedBy,
			Children:  lsGoodsClassService.GetLsGoodsClassChildrenList(db, IsGoodsClass),
		})
	}
	return g
}
